// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "requestHttpInfo.h"
#include "Interfaces/IHttpRequest.h"
#include "UObject/NoExportTypes.h"
#include "VerificationOfsignatureBase.h"
#include "simpleHttpObject.generated.h"




UCLASS(BlueprintType, Category = "SimpleHttpObj")
class SIMPLEHTTPREQUEST_API UMK_SimpleHttpObj :public UObject
{
	GENERATED_BODY()
	
private:

	/*当前状态**/
	EMK_SimpleHttpLifeTyp m_lifeTyp;

	/*上次请求数据缓存**/
	FMKSimpleHttpRequestData postDataBuffer;

	/*回调**/
	F_SimpleHttpServerReturn backEvent;


	TArray<uint8> contentData;
	   
	void OnCallBack(FString& back);

	void HttpCallBack(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);

	void SetHttpVerb(TSharedRef<class IHttpRequest, ESPMode::ThreadSafe >& request, EMK_SimpleHttpPostTyp typ);

public:

	UMK_SimpleHttpObj();

	/**下载的数据缓存*/
	UPROPERTY(BlueprintReadWrite, Category = "SimpleHttpObj")
		TArray<uint8> downloadbuffer;

	/**设置回调*/
	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		bool SetCallBackEvent_Bp(F_SimpleHttpServerReturn e);

	/**设置回调*/
	bool SetCallBackEvent(const F_SimpleHttpServerReturn& e);


	/**查询上次请求是否完成*/
	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		bool IsReplyOrIdle() { return m_lifeTyp == EMK_SimpleHttpLifeTyp::IsReplyOrIdle; }

	/*开始一次请求
	* @param	data				要请求的信息
	* @param	ess				    错误反馈
	* @param	forceStart			是否强制开始（无论上次请求是否返回）
	* RETURN						是否开始成功
	*/
	bool StartPost(const FMKSimpleHttpRequestData& data, FString& ess,bool forceStart = false);


	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		void AddPostContentData(const TArray<uint8>& data);



	bool StartPost(const FMKSimpleHttpRequestData& data
		, UVerificationOfsignatureBase* vstool
		, FString& ess, bool forceStart = false);


	/*开始一次请求
	* @param	data				要请求的信息
	* @param	ess				    错误反馈
	* @param	forceStart			是否强制开始（无论上次请求是否返回）
	* RETURN						是否开始成功
	*/
	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		bool StartPost_BP(const FMKSimpleHttpRequestData& data,FString& ess, bool forceStart = false);

	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		bool StartPostVS_BP(const FMKSimpleHttpRequestData& data
			, UVerificationOfsignatureBase* vstool
			, FString& ess, bool forceStart = false);

	//#if WITH_EDITOR
	//	virtual bool GetReferencedContentObjects(TArray<UObject*>& Objects) const override;
	//#endif

	//移除监听
	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		void ClearCallback();
	
	/**把自己添加到root中,不再会被回收*/
	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		void SelfAddTORoot();

	/**标记为可回收*/
	UFUNCTION(BlueprintCallable, Category = "SimpleHttpObj")
		void RemoveSelfFromRoot();

	//把所有未能从root中移除的httpobj移除
	//!!请不要随意调用
	 static void RemoveAllFromRoot();

};